package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2023-11-21
 * Time: 17:16
 */

//查找图书
public class FindOperation implements IOperation {

    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书:");
        //让用户输入书名信息:
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //遍历书架上的书籍信息(这里只需要遍历书架上已有书本数次)
        int currentNum = bookList.getBookNum();
        for (int i = 0; i < currentNum; i++) {
            //逐个判断name是否等于books的第i个元素
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)) {
                System.out.println("相关书籍信息:");
                //这里book类的打印已经重写过toString方法了
                System.out.println(bookList.getBooks(i));
                System.out.println();
                //java中，void修饰的方法可以出现 return空值
                return;
            }
        }
        //这里循环结束，如果执行到这里说明没有相同的书籍信息
        System.out.println("没有找到名字为《" + name + "》的书籍信息");
        System.out.println();
    }
}
